#include <app/aerApplication.hpp>
#include <app/aerWindow.hpp>
#include <app/aerEventHandler.hpp>
#include <view/aerFreeCamera.hpp>

#include "Terrain.hpp"


class Application : public aer::Application
{
  private:
    Terrain m_terrain;
    aer::FreeCamera *m_camera;
    
  public:
    Application() : aer::Application()
    {
      /// Window
      createWindow( aer::Window::sDisplay_GL33_800x600, "Organic terrain");
      m_Window->setVerticalSync( true );
      
      
      /// Camera
      aer::View view( aer::Vector3( 0.0f, 0.0f, 5.0f),
                      aer::Vector3( 0.0f, 0.0f, 0.0f),
                      aer::Vector3( 0.0f, 1.0f, 0.0f) );

      float ratio = m_Window->getDisplay().getAspectRatio();
      aer::Frustum frustum( 60.0f, ratio, 0.01f, 500.0f);

      m_camera = new aer::FreeCamera( view, frustum);
      m_camera->setMoveCoefficient( 0.05f );
      
      glClearColor( 0.15f, 0.15f, 0.15f, 1.0f);
      
      glswInit();
      glswSetPath( "../OrganicTerrain/shaders/", ".glsl");
      glswAddDirectiveToken( "*", "#version 420 core");
        m_terrain.init();      
      glswShutdown();
    }
    
    
    void updateFrame()
    {
      m_camera->update();
      
      if (aer::EventHandler::IsKeyPressed( sf::Keyboard::Escape )) {
        quit();
      }
      
      if (aer::EventHandler::IsKeyDown( sf::Keyboard::W )) {
        glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
      } else {
        glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
      }
      
    }
    
    void drawFrame()
    {
      glViewport( 0, 0, 800, 600); //
      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );//
      
      m_terrain.render(m_camera);
    }
};



int main(int argc, char *argv[])
{
  Application app;  
  app.run();
    
  return EXIT_SUCCESS;
}

